package com.sdu.cloudservice.action;

import com.sdu.cloudservice.dto.Envelope;
import com.sdu.cloudservice.service.SpiderService;
import com.sdu.cloudservice.service.SpiderStatus;
import com.sdu.cloudservice.token.AdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 6.26 klxlxf
 */
@Controller
@CrossOrigin
@RequestMapping("/spider")
public class SpiderAction {
    @Autowired
    private SpiderService spiderService;

    /**
     * 获取爬虫的历史信息
     * @param page
     * @return
     */
    @RequestMapping(value = "/history",method = RequestMethod.GET,produces="text/html;charset=UTF-8")
    @ResponseBody
    @AdminToken
    public String history(@RequestParam(name = "page",defaultValue = "1")int page){
        try {
            Envelope envelope = spiderService.getHistoryRecord(page);
            return envelope.toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
//        if(envelope.getCode()!=0){
//            return envelope.toString();
//        }

    }

    @RequestMapping("/historyNum")
    @ResponseBody
    @AdminToken
    public String historyNumber(){
        try{
            return spiderService.getHistoryNumber().toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
    }

    /**
     * 爬虫当前状态
     * @return 包含爬虫当前状态的json字符串
     */
    @RequestMapping("/status")
    @ResponseBody
    @AdminToken
    public String curStatus(){
        try{
            return new Envelope(SpiderStatus.getCurrentWorkingJson()).toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
    }

    /**
     * 修改爬虫频率
     * @param frequency 新频率(小时)
     * @return
     */
    @RequestMapping(value = "/updateFrequency",method = RequestMethod.POST)
    @ResponseBody
    @AdminToken
    public String updateFrequency(int frequency){
        return spiderService.updateFrequency(frequency).toString();
    }

    /**
     * 更新区域状态
     * @param regionIds 区域id列表jsonarray
     * @param level 新状态值
     * @return
     */
    @RequestMapping("/updateRegions")
    @ResponseBody
    @AdminToken
    public String updateRegions(String regionIds,int level){
        try{
//            System.out.println(regionIds);
            return spiderService.updateRegionValid(regionIds,level).toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
    }

    /**
     * 获取爬取地区状态
     * @return
     */
    @RequestMapping(value = "/regions")
    @ResponseBody
    @AdminToken
    public String getRegions(){
        try{
            return spiderService.getRegions().toString();
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.systemError.toString();
        }
    }

    /**
     * 强制爬虫工作
     * @return

    @RequestMapping(value = "/forceToWork",method = RequestMethod.POST)
    @ResponseBody
    @AdminToken
    public String forceToWork(){
        try{
            return spiderService.forceToWork().toString();
        }catch (Exception e){
            return Envelope.systemError.toString();
        }
    }
     */
}
